#

| Chapter 3 Moving Around in a Hurry |

## 3.3 Movement by Searches

`[/]` One of the most useful ways to move around in a large file quickly is by searching for text, or more properly, a pattern of characters. Sometimes a search can be performed to find a misspelled word or to find each occurrence of a variable in a program.

The search command is the special character `/` (slash). When you enter a slash, it appears on the bottom line of the screen; then type in the pattern that you want to find:

>     /pattern

A pattern can be a whole word or any other sequence of characters (called a "character string"). For example, if you search for the characters red, you will match "red" as a whole word, but you'll also match "occurred". If you include a space before or after pattern, the spaces will be treated as part of the word. As with all bottom-line commands, press `[RETURN]` to finish. (vi, like all other UNIX editors, has a special pattern-matching language that allows you to look for variable text patterns; for example, any word beginning with a capital letter, or the word The at the beginning of a line. We'll talk about this more powerful pattern-matching syntax in Chapter 6, Global Replacement . For right now, think of pattern simply as a word or phrase.)

vi begins the search at the cursor and searches forward, wrapping around to the start of the file if necessary. The cursor will move to the first occurrence of the pattern. If there is no match, the message "Pattern not found" will be shown on the status line.

Using the file practice, here's how to move the cursor by searches:

Keystrokes | Results
--- | ---
> `/edits` |
>     With a screen editor you can scroll the
>     page, move the cursor, delete lines, insert
>     characters, and more, while seeing the
>     results of your edits as you make them.
| Search for the pattern edits. Press RETURN to enter. The cursor moves directly to that pattern.
> `/scr` |
>     With a screen editor you can scroll the
>     page, move the cursor, delete lines, insert
>     characters, and more, while seeing the
>     results of your edits as you make them.
| Search for the pattern scr. Press RETURN to enter. Note that there is no space after scr.

The search wraps around to the front of the file. Note that you can give any combination of characters; a search does not have to be for a complete word.

To begin a search backward, type a `?` instead of a `/`:

>     ?pattern

In this case, the search wraps around to the end of the file, if necessary.

### 3.3.1 Repeating Searches

`[n]` The last pattern that you searched for stays available throughout your editing session. After a search, instead of repeating your original keystrokes, you can use a command to search again for the last pattern.

`n` | Repeat search in same direction.
`N` | Repeat search in opposite direction.
`/` `[RETURN]` | Repeat search forward.
`?` `[RETURN]` | Repeat search backward.

Since the last pattern stays available, you can search for a pattern, do some work, and then search again for the same pattern without retyping it by using `n`, `N`, `/` or `?`. The direction of your search (`/` is forward, `?` is backward) is displayed at the bottom left of the screen.

To continue with the example above, since the pattern scr is still available for search, you can:

Keystrokes | Results
--- | ---
> `n` |
>     With a screen editor you can scroll the
>     page, move the cursor, delete lines, insert
>     characters, and more, while seeing the
>     results of your edits as you make them.
| Move to the next instance of the pattern scr (from screen to scroll) with the `n` (next) command.
> `?you` |
>     With a screen editor you can scroll the
>     page, move the cursor, delete lines, insert
>     characters, and more, while seeing the
>     results of your edits as you make them.
| Search backward with `?` from the cursor to the first occurrence of you. You need to press RETURN after typing the pattern.
> `N` |
>     With a screen editor you can scroll the
>     page, move the cursor, delete lines, insert
>     characters, and more, while seeing the
>     results of your edits as you make them.
| Repeat previous search for you but in the opposite direction (forward).

Sometimes you want to find a word only if it is further ahead; you don't want the search to wrap around earlier in the file. vi has an option, `wrapscan`, that controls whether searches wrap. You can disable wrapping like this:

>     :set nowrapscan

When `nowrapscan` is set and a forward search fails, the status line displays the message:

>     Address search hit BOTTOM without matching pattern

When `nowrapscan` is set and a backward search fails, the message displays "TOP" instead of "BOTTOM".

This section has given only the barest introduction to searching for patterns. Chapter 6, Global Replacement, will teach more about pattern matching and its use in making global changes to a file.

### 3.3.2 Current Line Searches

`[f]` There are also miniature versions of the search commands that operate within the current line. The command `f` x moves the cursor to the next instance of the character x (where x stands for any character). The command `t` x moves the cursor to the character before the next instance of x. Semicolons can then be used repeatedly to "find" your way along.

The in-line search commands are summarized below. None of these commands will move the cursor to the next line.

`f` x | Find (move cursor to) next occurrence of x in the line, where x stands for any character.
`F` x | Find (move cursor to) previous occurrence of x in the line.
`t` x | Find (move cursor to) character before next occurrence of x in the line.
`T` x | Find (move cursor to) character after previous occurrence of x in the line.
`;` | Repeat previous find command in same direction.
`,` | Repeat previous find command in opposite direction.

With any of these commands, a numeric prefix n will locate the nth occurrence. Suppose you are editing in practice, on this line:

>     With a screen editor you can scroll the

Keystrokes | Results
--- | ---
> `fo` |
>     With a screen editor you can scroll the
| Find the first occurrence of o in your current line with `f.`
> `;` |
>     With a screen editor you can scroll the
| Move to the next occurrence of o with the `;` command (find next o).

`df` x deletes up to and including the named character x. This command is useful in deleting or yanking partial lines. You might need to use `df` x instead of `dw` if there were symbols or punctuation within the line that made counting words difficult. The `t` command works just like `f`, except that it positions the cursor before the character searched for. For example, the command `ct.` could be used to change text up to the end of a sentence, leaving the period.

3.2 Movement by Text Blocks |  | 3.4 Movement by Line Number